package pers.gxm.strategy;

import pers.gxm.factorymethod.framework.Product;

public class ProbStrategy implements Strategy{
    private boolean won;
    private int preHandValue;
    private int currentHandValue;
    /**
     * history代表前一次出拳和这一次出拳的获胜次数，默认值为1，行为前一次出拳，列为后一次出拳。
     */
    private int[][] history = new int[3][3];
    public ProbStrategy(){
        for(int i = 0;i < history.length;i++){
            for(int j = 0;j < history[0].length;j++){
                history[i][j] = 1;
            }
        }
    }
    @Override
    public Hand nextHand() {
        int sum = getSum(preHandValue);
        int randomValue = (int)(Math.random() * sum);
        int handValue = 0;
        if(randomValue < history[preHandValue][0]){
            handValue = 0;
        }else if(history[preHandValue][0] <= randomValue && randomValue <= history[preHandValue][1] ){
           handValue = 1;
        }else{
            handValue = 2;
        }
        preHandValue = currentHandValue;
        currentHandValue = handValue;
        return Hand.getHand(handValue);
    }


    private int getSum(int hv){
        int sum = 0;
        for(int i = 0;i < history[hv].length;i++){
           sum += history[hv][i];
        }
        return sum;
    }

    /**
     * 对局结束，根据结果更新数组，如果为平局则不更新
     * @param win
     */
    @Override
    public void study(boolean win) {
        if(win){
            history[preHandValue][currentHandValue]++;
        }else{
            history[preHandValue][(currentHandValue + 1) % 3]++;
            history[preHandValue][(currentHandValue + 2) % 3]++;
        }
    }
}
